package com.bonc.modules.job.task;

import cn.hutool.core.date.DateUtil;
import com.bonc.modules.rpt.service.SunshineActionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;

/**
 * @author jinlong
 * @date 2021/8/20
 */
@Slf4j
@Component("sunshineActionTask")
public class SunshineActionTask implements ITask {
    private static final String LOCK_NAME = "sunshine_action_task";

    @Autowired
    private RedisLockRegistry redisLockRegistry;

    @Autowired
    private SunshineActionService sunshineActionService;

    @Override
    public void run(String params) throws Exception {

        Lock lock = redisLockRegistry.obtain(LOCK_NAME);
        boolean isLock = lock.tryLock();
        if (isLock) {
            try {
                log.info("阳光行动统计:开始time:{}", DateUtil.date());
                sunshineActionService.calcCount();
                sunshineActionService.calcMom();
                sunshineActionService.calcDutyIncrease();
                log.info("阳光行动统计:结束time:{}", DateUtil.date());
            } finally {
                lock.unlock();
            }
        }

    }
}
